package com.xintouyun.medicine.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xintouyun.medicine.entity.Goods;
import com.xintouyun.medicine.service.IndexGoodsShowService;
import com.xintouyun.medicine.service.TypeService;
import com.xintouyun.medicine.service.impl.IndexGoodsShowServiceImpl;
import com.xintouyun.medicine.service.impl.TypeServiceImpl;

@WebServlet("/searchgoods")
public class SearchGoodsServlet extends HttpServlet{
	@Override
    public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
		
		TypeService typeService = new TypeServiceImpl();
		IndexGoodsShowService indexGoodsShowService = new IndexGoodsShowServiceImpl();
    	String typeid = request.getParameter("type_id");
    	String nextpage = request.getParameter("pageIndex");
    	if(nextpage == null) {
    		nextpage = "1";
    	}
    	int pageIndex = Integer.parseInt(nextpage);
    	if(typeid != null) {
			int tid = Integer.parseInt(typeid);
			List<Goods> searchGoods = typeService.searchGoods(tid,pageIndex,20);		
		    request.setAttribute("searchGoods", searchGoods);	
    	}else {
    		String keywords = request.getParameter("keywords");
    		List<Goods> searchGoods = indexGoodsShowService.keywordsGoods(keywords,pageIndex,5);
    		request.setAttribute("searchGoods", searchGoods);
    	}
    	request.setAttribute("pageIndex", pageIndex);
    	request.setAttribute("type_id", typeid);
		request.getRequestDispatcher("searching.jsp").forward(request, response);
	}
	
	@Override
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	this.doGet(request, response);
    }
}
